package edu.fudan.jwxt.controller;

import edu.fudan.jwxt.annotation.Adminer;
import edu.fudan.jwxt.common.Constants;
import edu.fudan.jwxt.common.ResultBody;
import edu.fudan.jwxt.dto.UserDto;
import edu.fudan.jwxt.entity.ClassTime;
import edu.fudan.jwxt.service.ClassTimeService;
import edu.fudan.jwxt.utils.RoleCheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/classtime")
public class ClassTimeController {

    @Autowired
    private ClassTimeService classTimeService;

    @GetMapping
    public ResultBody ListAll(){
        List<ClassTime> classTimeList =  classTimeService.list();

        return ResultBody.success(classTimeList);
    }


    @GetMapping("/{id}")
    public ResultBody getById(@PathVariable int id){
        ClassTime classTime = classTimeService.getById(id);

        return ResultBody.success(classTime);
    }

    //TODO: 用户权限检查
    @PostMapping("/insert")
    public ResultBody insertTime(@RequestBody ClassTime classTime){
        classTimeService.save(classTime);

        return ResultBody.success();
    }

    //TODO: 用户权限检查
    @PostMapping("/update")
    public ResultBody updateTime(@RequestBody ClassTime classTime){
        boolean flag = classTimeService.updateById(classTime);

        if(flag)
            return ResultBody.success();
        else
            return ResultBody.error(Constants.CODE_600,"课节不存在");
    }

    //TODO: 用户权限检查
    @DeleteMapping("/{id}")
    public ResultBody deleteTime(@PathVariable int id){
        boolean flag = classTimeService.removeById(id);

        if(flag)
            return ResultBody.success();
        else
            return ResultBody.error(Constants.CODE_600,"课节不存在");
    }

    //批量删除
    //TODO: 用户权限检查
    @PostMapping("/delete")
    public ResultBody DeleteList(@RequestBody List<ClassTime> classTimeList){
        RoleCheckUtil.CheckUserAdmin();

        classTimeService.removeByIds(classTimeList);
        return ResultBody.success();
    }

}
